<template>
  <div class="tab-list-comp">
    <div class="tab-list flex align-center">
      <div
        v-for="(item, index) in list"
        :key="item.name"
        class="tab-item pointer"
        :class="{ active: activeIndex === index }"
        @click="toggle(index)"
      >
        <slot :item="item">
          {{ item.name }}
        </slot>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'TabList',
  props: {
    list: {
      type: Array,
      default() {
        return []
      }
    },
    active: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      activeIndex: 0
    }
  },
  watch: {
    active() {
      this.activeIndex = this.active
    }
  },
  created() {
    this.activeIndex = this.active
  },
  methods: {
    toggle(index) {
      this.activeIndex = index
      this.$emit('get', index, this.list[index])
    }
  }
}
</script>
<style lang="scss" scoped>
.tab-list {
  padding: 3px 4px;
  .tab-item {
    position: relative;
    font-size: 14px;
    font-weight: 500;
    color: rgba(110, 110, 110, 1);
    box-sizing: border-box;
    padding: 6px 0;
    position: relative;
    width: fit-content;
    margin-right: 50px;
    &::after {
      content: "";
      display: block;
      width: 100px;
      height: 3px;
      background-color: transparent;
      bottom: 4px;
      left: 0;
      right: 0;
      margin-left: auto;
      margin-right: auto;
      position: absolute;
    }
  }
  .tab-item.active {
    position: relative;
    &::after {
      content: "";
      display: block;
      width: 100px;
      height: 3px;
      bottom: 4px;
      left: 0;
      right: 50px;
      margin-left: auto;
      margin-right: auto;
      position: absolute;
      background-color: rgba(0, 119, 255, 1);
    }
  }
}
.dark {
  .tab-list {
    background: rgba(230, 237, 245, 0.4);
    .tab-item {
      color: rgba(255, 255, 255, 0.5);
    }
    .tab-item.active {
      color: #fff;
    }
  }
}
</style>
